package com.rqf.ls.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.rqf.ls.service.IVerifyOfLawyerService;
import com.server.date.DateUtil;

/**
 * 验证码缓存服务
 * @author ServerZhang
 * @date 2016年3月17日
 */
@Service("VerifyOfLawyerService")
public class VerifyOfLawyerService implements IVerifyOfLawyerService {
	
	private static long register_outtime = 600000;//10分钟
	private static long login_outtime = 600000;//10分钟
	private static long pswUpdate_outtime = 600000;//10分钟
	
	//注册验证码
	private static Map<String, Map<String, Object>> registerCode = new HashMap<String, Map<String, Object>>();
	//登录验证码
	private static Map<String, Map<String, Object>> loginCode = new HashMap<String, Map<String, Object>>();
	//修改密码验证码
	private static Map<String, Map<String, Object>> pswUpdateCode = new HashMap<String, Map<String, Object>>();
	
	
	
	/**
	 * 验证码过期清理
	 * @author ServerZhang
	 * @date 2016年3月17日
	 */
	@Override
	public void clean(){
		this.cleanRegisterCode();
		this.cleanLoginCode();
		this.cleanPswUpdateCode();
	}
	@Override
	public void cleanRegisterCode(){
		Set<String> keySet = registerCode.keySet();
		for(String key : keySet){
			Map<String, Object> valueMap = registerCode.get(key);
			if (valueMap != null && valueMap.size() > 0) {
				Object codeObj = valueMap.get("code");//验证码
				Object timeObj = valueMap.get("time");//生成时间
				if (codeObj != null && timeObj != null) {
					Date date = (Date)timeObj;
					long dateLong = date.getTime();
					Date now = Calendar.getInstance().getTime();
					long nowLong = now.getTime();
					long devValue = nowLong - dateLong;
					
					//TODO 获取配置文件中的注册码超时分钟数
					Properties properties = new Properties();
					
					//test
					
					//超时
					if (devValue > this.register_outtime) {
						valueMap.remove(key);
					}
				}else {
					valueMap.remove(key);
				}
			}else {
				registerCode.remove(key);
			}
		}
	}
	@Override
	public void cleanLoginCode(){
		// 修改时验证码操作方式同上
		Set<String> updateKeySet = loginCode.keySet();
		for(String key : updateKeySet){
			Map<String, Object> valueMap = loginCode.get(key);
			if (valueMap != null && valueMap.size() > 0) {
				Object codeObj = valueMap.get("code");//验证码
				Object timeObj = valueMap.get("time");//生成时间
				if (codeObj != null && timeObj != null) {
					Date date = (Date)timeObj;
					long dateLong = date.getTime();
					Date now = Calendar.getInstance().getTime();
					long nowLong = now.getTime();
					long devValue = nowLong - dateLong;
					
					//TODO 获取配置文件中的注册码超时分钟数
					Properties properties = new Properties();
					
					//test
					
					//超时
					if (devValue > this.login_outtime) {
						valueMap.remove(key);
					}
				}else {
					valueMap.remove(key);
				}
			}else {
				loginCode.remove(key);
			}
		}
	}
	@Override
	public void cleanPswUpdateCode(){
		// 修改密码时验证码操作方式同上
		Set<String> updateKeySet = pswUpdateCode.keySet();
		for(String key : updateKeySet){
			Map<String, Object> valueMap = pswUpdateCode.get(key);
			if (valueMap != null && valueMap.size() > 0) {
				Object codeObj = valueMap.get("code");//验证码
				Object timeObj = valueMap.get("time");//生成时间
				if (codeObj != null && timeObj != null) {
					Date date = (Date)timeObj;
					long dateLong = date.getTime();
					Date now = Calendar.getInstance().getTime();
					long nowLong = now.getTime();
					long devValue = nowLong - dateLong;
					
					//TODO 获取配置文件中的注册码超时分钟数
					Properties properties = new Properties();
					
					//test
					
					//超时
					if (devValue > this.pswUpdate_outtime) {
						valueMap.remove(key);
					}
				}else {
					valueMap.remove(key);
				}
			}else {
				pswUpdateCode.remove(key);
			}
		}
	}
	
	 /**
	 * 增加或修改手机号对应验证码及验证时间
	 * @param phone
	 * @param code
	 * @author ServerZhang
	 * @date 2016年3月17日
	 */
	@Override
	public void addRegisterCode(String phone, String code){
		Map<String, Object> map = this.registerCode.get(phone);
		if (map != null) {
			
		}else {
			map = new HashMap<String, Object>();
		}
		map.put("code", code);
		map.put("time", DateUtil.getDateWithSeconds(new Date()));
		this.registerCode.put(phone, map);
	}
	@Override
	public void addLoginCode(String phone, String code){
		Map<String, Object> map = this.loginCode.get(phone);
		if (map != null) {
			
		}else {
			map = new HashMap<String, Object>();
		}
		map.put("code", code);
		map.put("time", DateUtil.getDateWithSeconds(new Date()));
		this.loginCode.put(phone, map);
	}
	@Override
	public void addPswUpdateCode(String phone, String code){
		Map<String, Object> map = this.pswUpdateCode.get(phone);
		if (map != null) {
			
		}else {
			map = new HashMap<String, Object>();
		}
		map.put("code", code);
		map.put("time", DateUtil.getDateWithSeconds(new Date()));
		this.pswUpdateCode.put(phone, map);
	}
	 /**
	 * 删除验证码
	 * @param phone
	 * @author ServerZhang
	 * @date 2016年3月17日
	 */
	@Override
	public void deleteRegisterCode(String phone){
		Map<String, Object> map = this.registerCode.get(phone);
		if (map != null) {
			this.registerCode.remove(phone);
		}
	}
	@Override
	public void deleteLoginCode(String phone){
		Map<String, Object> map = this.loginCode.get(phone);
		if (map != null) {
			this.loginCode.remove(phone);
		}
	}
	@Override
	public void deletePswUpdateCode(String phone){
		Map<String, Object> map = this.pswUpdateCode.get(phone);
		if (map != null) {
			this.pswUpdateCode.remove(phone);
		}
	}
	 /**
	 * 获取验证码
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月17日
	 */
	@Override
	public Map<String, Object> getRegisterCode(String phoneNo){
		return this.registerCode.get(phoneNo);
	}
	@Override
	public Map<String, Object> getLoginCode(String phoneNo){
		return this.loginCode.get(phoneNo);
	}
	@Override
	public Map<String, Object> getPswUpdateCode(String phoneNo){
		return this.pswUpdateCode.get(phoneNo);
	}
	
}
